package com.xsir.ditan.admin.controller;

import com.xsir.ditan.admin.vo.GoodsInfoVo;
import com.xsir.ditan.miniprogram.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * @author user
 * @date 2021/2/3
 */
@Slf4j
@Controller
@RequestMapping("goods")
public class GoodsController extends BaseController {
    
    @Autowired
    private IGoodsService goodsService;
    
    @PostMapping("/list")
    @ResponseBody
    public Map<String, Object> list() {
        return goodsService.getList();
    }
    
    
    /**
     * 获取记录详情
     *
     * @param id    记录ID
     * @param model 模型
     * @return
     */
    @Override
    public String edit(Integer id, Model model) {
        Map<String, Object> info = new HashMap<>();
        if (id != null && id > 0) {
            info = goodsService.info(id);
        }
        model.addAttribute("info", info);
        return super.edit(id, model);
    }
    
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody GoodsInfoVo goodsInfoVo) {
        return goodsService.edit(goodsInfoVo);
    }
    
    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Integer id) {
        return goodsService.delete(id);
    }
    
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody GoodsInfoVo goodsInfoVo) {
        return goodsService.edit(goodsInfoVo);
    }
}
